// preload.js
const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('electronAPI', {
  // onOpenUrl 该对象能被渲染进程（index.html）调用，当调用时是通过window.electronAPI.onOpenUrl(function(value){})这样的方式回调的
  onOpenUrl: (callback) => ipcRenderer.on('open-url', (_event, url) => callback(url)), // 这个回调就相当于在渲染进程执行了它提交的匿名函数
})

// All the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const dependency of ['chrome', 'node', 'electron']) {
    replaceText(`${dependency}-version`, process.versions[dependency])
  }

  console.log('window.location.href')
  console.log(window.location.href)


})